package com.higgins.client.knowledge.trainer;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.higgins.client.knowledge.KnowledgeItemDTO;
import com.higgins.client.knowledge.KnowledgeService;
import com.higgins.client.knowledge.KnowledgeServiceAsync;

public class KnowledgeSubjectListBox extends ListBox	{
	private final KnowledgeServiceAsync knowledgeService = GWT
			.create(KnowledgeService.class);
	private List<KnowledgeItemDTO> listKnowledgeItemDTOs;

	public KnowledgeSubjectListBox()	{
		super();
		initialise();
	}
	private void initialise()	{
		knowledgeService.retrieveTopLevelKnowledgeItems(new AsyncCallback<List<KnowledgeItemDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<KnowledgeItemDTO> results) {
				addItem("[Subject]");
				listKnowledgeItemDTOs = results;
				for (KnowledgeItemDTO dto:results)	{
					addItem(dto.getItem());
				}
			}
		});
	}
	public Long getId()	{
		int id = getSelectedIndex();
		if (id<1)
			return null;
		else 	{
			KnowledgeItemDTO dto = listKnowledgeItemDTOs.get(id-1);
			return dto.getId();
		}
	}
	
}
